using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;
using System.Xml.Serialization;
using System.Threading;
using System.Net;
using Microsoft.Win32;
using System.Diagnostics;
using System.Configuration;
using System.Security;
using System.Security.Cryptography;
using System.Net.NetworkInformation;
using System.Data;
using System.Globalization;
using System.Xml.Linq;

namespace Common
{
    public class Utility
    {
        public static string GetApplicationConnectionString()
        {
            return ConfigurationManager.AppSettings[ConfigProperties.APPCONSTRING].ToString();
        }
        #region DATA PROCESSING
        public static int? GetDataReaderIntegerFieldValue(IDataReader oReader, string field)
        {
            int result;
            bool isNum;

            if (oReader[field] == null)
            {
                return null;
            }

            isNum = int.TryParse(oReader[field].ToString(), out result);

            if (isNum)

                return result;
            else
                return null;
        }

        public static bool? GetDataReaderBoolFieldValue(IDataReader oReader, string field)
        {
            bool result;
            bool isBool;

            if (oReader[field] == null)
            {
                return null;
            }

            isBool = bool.TryParse(oReader[field].ToString(), out result);

            if (isBool)
            {
                return result;
            }
            else
            {
                return null;
            }
        }

        public static int? GetStringToIntegerNullValue(object value)
        {
            int result;
            bool isNum;

            if (value == null)
            {
                return null;
            }

            isNum = int.TryParse(value.ToString(), out result);

            if (isNum)

                return result;
            else
                return null;
        }

        public static bool? GetStringToBoolNullValue(object value)
        {
            bool result;
            bool isNum;

            if (value == null)
            {
                return null;
            }

            isNum = bool.TryParse(value.ToString(), out result);

            if (isNum)

                return result;
            else
                return null;
        }
        public static decimal? GetDataReaderDecimalFieldValue(IDataReader oReader, string field)
        {
            decimal result;
            bool isNum;

            if (oReader[field] == null)
            {
                return null;
            }

            isNum = decimal.TryParse(oReader[field].ToString(), out result);

            if (isNum)

                return result;
            else
                return null;
        }
        public static DateTime? GetDataReaderDateTimeFieldValue(IDataReader oReader, string field)
        {
            DateTime result;
            bool isDate;

            if (oReader[field] == null)
            {
                return null;
            }

            isDate = DateTime.TryParse(oReader[field].ToString(), out result);

            if (isDate)

                return result;
            else
                return null;
        }
        #endregion

        public static string GetErrorMessageTitle()
        {
            return AppDomain.CurrentDomain.FriendlyName;
        }
        /// <summary>
        /// Log error
        /// </summary>
        /// <param name="ex"></param>
        public static void LogEmail(Exception ex)
        {
            //
        }
        public static string GetExceptionMessageString(Exception ex)
        {
            string str = string.Empty;
            string innerException = string.Empty;
            try
            {
                str = ex.Message + Environment.NewLine + Environment.NewLine;
                str = str + "Source: " + ex.Source + Environment.NewLine + Environment.NewLine;
                str = str + "StackTrace: " + ex.StackTrace.ToString() + Environment.NewLine + Environment.NewLine;

                if (ex.InnerException != null)
                {
                    innerException = GetExceptionMessageString(ex.InnerException);
                    str = str + "InnerException: " + innerException;
                }
                return str;
            }
            catch
            {
                return str;
            }
        }
    }

}
